﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynaPrecision.LinearSystem
{
	public class GaussSeidelIterativeMethod<TData> : IIterativeMethod<TData>
	{

		#region IIterativeMethod<TInput> Members

		public void NextResult<TInput, TResult, Tload>(TInput input, Tload load, TResult thisStep, ref TResult nextStep)
			where TInput : IMultiDimensionalArray<TData>
			where TResult : IMultiDimensionalArray<TData>
			where Tload : IMultiDimensionalArray<TData>
		{
			for (int i = 0; i < input.GetLength(1); i++)
			{
				nextStep[i, 0] = load[i, 0];
				for (int j = 0; j < i; j++)
				{
					nextStep[i, 0] = nextStep[i, 0] - input[i, j] * (dynamic)nextStep[j, 0];
				}
				for (int j = i + 1; j < input.GetLength(0); j++)
				{
					nextStep[i, 0] = nextStep[i, 0] - input[i, j] * (dynamic)thisStep[j, 0];
				}
				nextStep[i, 0] = nextStep[i, 0] / (dynamic)input[i, i];
			}
		}

		#endregion
	}
}
